package com.glory.erp.auth.emp.action;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.List;
import java.util.Random;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.aspectj.weaver.NewParentTypeMunger;

import com.glory.erp.auth.base.BaseAction;
import com.glory.erp.auth.dept.bean.Dept;
import com.glory.erp.auth.dept.service.DeptService;
import com.glory.erp.auth.emp.bean.Emp;
import com.glory.erp.auth.emp.bean.EmpQuery;
import com.glory.erp.auth.emp.service.EmpService;
import com.glory.erp.auth.resources.bean.Resources;
import com.glory.erp.auth.resources.service.ResourcesService;
import com.glory.erp.auth.role.bean.Role;
import com.glory.erp.auth.role.service.RoleService;
import com.glory.erp.util.VerificationCodeUtil;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

/**
 * @Description: 员工控制层
 * @Title: EmpAction.java
 * @Author: 樊双峰
 * @Date: 2018年5月18日 上午12:54:13
 * @Version V1.0
 */
public class EmpAction extends BaseAction {

	public Emp emp = new Emp();
	public EmpQuery empQuery = new EmpQuery();
	public String newPassword1;
	public String newPassword2;
	public String verificationCode;
	public Long[] roleUuids;

	private EmpService empService;
	private DeptService deptService;
	private RoleService roleService;
	private ResourcesService resourcesService;

	public void setResourcesService(ResourcesService resourcesService) {
		this.resourcesService = resourcesService;
	}

	public void setRoleService(RoleService roleService) {
		this.roleService = roleService;
	}

	public void setEmpService(EmpService empService) {
		this.empService = empService;
	}

	public void setDeptService(DeptService deptService) {
		this.deptService = deptService;
	}

	/**
	 * 
	 * @Description: 员工信息展示
	 * @Title: list
	 * @Author: 樊双峰
	 * @Param: @return
	 * @return: String
	 * @Date: 2018年5月20日 上午12:13:58
	 * @throws
	 */
	public String list() {
		List<Dept> deptList = deptService.findAll();
		put("deptList", deptList);
		setDataTotal(empService.getCount(empQuery));
		List<Emp> empList = empService.findAll(empQuery, pageNum, pageCount);
		put("empList", empList);
		return LIST;
	}

	/**
	 * 
	 * @Description: 跳转至员工添加、修改页面
	 * @Title: add
	 * @Author: 樊双峰
	 * @Param: @return
	 * @return: String
	 * @Date: 2018年5月20日 上午1:59:37
	 * @throws
	 */
	public String addView() {
		List<Role> roleList = roleService.findAll();
		put("roleList", roleList);
		List<Dept> deptList = deptService.findAll();
		put("deptList", deptList);
		if (emp.getUuid() != null) {
			emp = empService.findById(emp.getUuid());
			// roleUuids 中暂时无数据，必须对其初始化，，否组会抛空指针
			roleUuids = new Long[emp.getRoles().size()];
			// set -> array
			int i = 0;
			for (Role r : emp.getRoles()) {
				roleUuids[i++] = r.getUuid();
			}
		}
		return ADD;
	}

	/**
	 * 
	 * @Description: 员工信息添加、修改
	 * @Title: saveAndUpdate
	 * @Author: 樊双峰
	 * @Param: @return
	 * @return: String
	 * @Date: 2018年5月20日 上午2:37:57
	 * @throws
	 */
	public String saveAndUpdate() {
		if (emp.getUuid() == null) {
			empService.save(emp, roleUuids);
		} else {
			empService.update(emp, roleUuids);
		}
		return TO_LIST;
	}

	/**
	 * 
	 * @Description: 删除员工
	 * @Title: delete
	 * @Author: 樊双峰
	 * @Param: @return
	 * @return: String
	 * @Date: 2018年5月20日 上午3:30:12
	 * @throws
	 */
	public String delete() {
		empService.delete(emp);
		return TO_LIST;
	}

	/**
	 * 
	 * @Description: 员工登录
	 * @Title: login
	 * @Author: 樊双峰
	 * @Param: @return
	 * @return: String
	 * @Date: 2018年5月19日 上午2:02:06
	 * @throws
	 */
	public String login() {
		HttpServletRequest request = getRequest();
		String code = (String) request.getSession().getAttribute("verificationCode");
		String loginIp = request.getHeader("x-forwarded-for");
		if(verificationCode == null || verificationCode == "") {
			this.addActionError("对不起，验证码不能为空！");
			return "loginFail";
		} else {
			if(!(verificationCode.equalsIgnoreCase(code))) {
				this.addActionError("对不起，验证码输入错误！");
				return "loginFail";
			} else {
				if (loginIp == null || loginIp.length() == 0
						|| "unknown".equalsIgnoreCase(loginIp)) {
					loginIp = request.getHeader("Proxy-Client-IP");
				}
				if (loginIp == null || loginIp.length() == 0
						|| "unknown".equalsIgnoreCase(loginIp)) {
					loginIp = request.getHeader("WL-Proxy-Client-IP");
				}
				if (loginIp == null || loginIp.length() == 0
						|| "unknown".equalsIgnoreCase(loginIp)) {
					loginIp = request.getRemoteAddr();
				}
				Emp empLogin = empService.login(emp.getUserName(), emp.getPassword(),
						loginIp);
				if (empLogin == null) {
					this.addActionError("对不起，用户名密码错误！");
					return "loginFail";
				} else {
					List<Resources> resourcesList = resourcesService
							.findAllByEmp(empLogin.getUuid());
					StringBuilder sb = new StringBuilder();
					for (Resources r : resourcesList) {
						sb.append(r.getUrl());
						sb.append(",");
					}
					empLogin.setResList(sb.toString());
					ActionContext.getContext().getSession().put(emp.EMPINFO, empLogin);
					return "loginSuccess";
				}
			}
		}
	}

	/**
	 * 
	 * @Description: 登出
	 * @Title: logout
	 * @Author: 樊双峰
	 * @Param: @return
	 * @return: String
	 * @Date: 2018年5月20日 下午12:16:03
	 * @throws
	 */
	public String logout() {
		putSession(emp.EMPINFO, null);
		return "noLogin";
	}

	/**
	 * 
	 * @Description: 跳转至修改密码页面
	 * @Title: toChangePwd
	 * @Author: 樊双峰
	 * @Param: @return
	 * @return: String
	 * @Date: 2018年5月20日 下午12:29:02
	 * @throws
	 */
	public String toChangePwd() {
		return "toChangePwd";
	}

	/**
	 * 
	 * @Description: 修改密码
	 * @Title: changePwd
	 * @Author: 樊双峰
	 * @Param: @return
	 * @return: String
	 * @Date: 2018年5月20日 下午12:39:29
	 * @throws
	 */
	public String changePwd() {
		boolean flag = empService.changePwd(getLogin().getUserName(),
				emp.getPassword(), newPassword1, newPassword2);
		if (flag) {
			// 修改成功
			putSession(Emp.EMPINFO, null);
			return "noLogin";
		} else {
			// 修改失败
			this.addActionError("修改失败！");
			return "toChangePwd";
		}
	}
}